//--------------------Sonic CD HUD Script---------------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
// Not all of these are used here in the HUD script, some are just used as a way for other objects to globally transmit values
private alias Object.Value0 : Object.Timer     // Initially set via a loop in the UFO script
private alias Object.Value1 : Object.MangleTimer     // Initially set via a loop in the UFO script
private alias Object.Value3 : Object.MangleRandom    // Initially set via a loop in the UFO script
private alias Object.Value4 : Object.MangleAI    // Initially set via a loop in the UFO script
private alias Object.Value5 : Object.ScareTimer    // Initially set via a loop in the UFO script

private alias Object.Value6 : Object.MangleRise    // Initially set via a loop in the UFO script
private alias Object.Value7 : Object.MoveTime    // Initially set via a loop in the UFO script
private alias Object.Value8 : Object.RandomLocation1    // Initially set via a loop in the UFO script

private alias Object.Value9 : Object.VentState    // Initially set via a loop in the UFO script
private alias Object.Value10 : Object.HeadShake    // Initially set via a loop in the UFO script
private alias Object.Value11 : Object.VentLocation    // Initially set via a loop in the UFO script

private alias Object[2].Value2 : Object.ScrollPos

// Priority
private alias 1 : PRIORITY_ACTIVE

private alias 0 : CAM_SHOWSTAGE_0
private alias 1 : CAM_DININGHALL_1
private alias 2 : CAM_BACKSTAGE_2
private alias 3 : CAM_PIRATESCOVE_3
private alias 4 : CAM_WESTHALL_4
private alias 5 : CAM_CLOSET_5
private alias 6 : CAM_WHALLCORNER_6
private alias 7 : CAM_EASTHALL_7
private alias 8 : CAM_EHALLCORNER_8
private alias 9 : CAM_KITCHEN_9
private alias 10 : CAM_BATHROOMS_10

private alias 15 : OFFICE_ATDOOR
private alias 16 : OFFICE_JUMPSCARES

private alias object.state : Mangle.State

private alias 2  : CAMERA_MAIN
private alias 10  : CAMERA_EXIT

//Vent States
private alias 0 : VENT_START

private alias 1 : VENT_TOPLEFT_1
private alias 2 : VENT_TOPMIDLEFT_2
private alias 3 : VENT_TOPMIDDLE_3
private alias 4 : VENT_TOPMIDRIGHT_4
private alias 5 : VENT_TOPRIGHT_5

private alias 6 : VENT_MIDLEFT_6
private alias 7 : VENT_MIDMIDLEFT_7
private alias 8 : VENT_MIDDLEMIDDLE_8
private alias 9 : VENT_MIDMIDRIGHT_9
private alias 10 : VENT_MIDRIGHT_10

private alias 11 : VENT_BOTTOMLEFT_11
private alias 12 : VENT_BOTTOMMIDLEFT_12
private alias 13 : VENT_BOTTOMMIDDLE_13
private alias 14 : VENT_BOTTOMMIDRIGHT_14
private alias 15 : VENT_BOTTOMRIGHT_15

private alias 16 : VENT_MIDDOOR
private alias 17 : VENT_LEFTDOOR
private alias 18 : VENT_RIGHTDOOR



event ObjectMain
//-1 is basically the ai level 0 ig

if Object[5].Type == TypeName[Blank Object]
	switch Mangle.state
	case 15
		Object.MangleTimer++
		Object.MoveTime = 700
		rand(temp3, 100)
		Object.MoveTime += temp3
		if Object.MangleTimer >= Object.MoveTime 
			StopSfx(SfxName[Garble])
			StopMusic()
			PlaySfx(SfxName[Scream2], 0)		
			Object.MangleRise = -280	
			object.state = 17
		end if	
	break
	case 16	//wa
		StopMusic()
		PlaySfx(SfxName[SpringScream], 0)			
		object.state++
	break	
	case 17
		StopSfx(SfxName[Garble])
		PlayerJumpscared = 1
		if Object.MangleRise < -100
			Object.MangleRise += 24
		else
			Object.MangleRise = -100
		end if
	break	
	case 1
		Object.MangleTimer++
		if Object.MangleTimer >= 200

			
			switch Object.VentState
			case VENT_MIDDOOR
				if MidVentDoor == 1
					object.state = 0
					object.VentState = VENT_START
					PlaySfx(SfxName[Block], 0)
				else
					PlaySfx(SfxName[VentMove], 0)		
					object.VentState = VENT_BOTTOMMIDDLE_13	
				end if						
			break
			case VENT_LEFTDOOR
				if LeftVentDoor == 1
					object.state = 0
					object.VentState = VENT_START
					PlaySfx(SfxName[Block], 0)
				else
					PlaySfx(SfxName[VentMove], 0)		
					object.VentState = VENT_BOTTOMMIDDLE_13	
				end if
			break
			case VENT_RIGHTDOOR
				if RightVentDoor == 1
					object.state = 0
					object.VentState = VENT_START
					PlaySfx(SfxName[Block], 0)
				else
					PlaySfx(SfxName[VentMove], 0)		
					object.VentState = VENT_BOTTOMMIDDLE_13	
				end if							
			break	

			//your gonna die ig
			case VENT_BOTTOMMIDDLE_13	
				Mangle.State = 2
			break
			end switch

			Object.MangleTimer = 0
		end if	
	break
	case 2	//at your door
	//not leaving lol
		if MiddleDoorClosed == 1
			Object.MangleTimer = 0
		else
			Object.MangleTimer++
			if Object.MangleTimer >= 300
				PlaySfx(SfxName[Garble], 1)		
				object.MangleTimer = 0
				object.state = 15
			end if
		end if	
	break
	case 0
		Rand(Object.VentLocation, 3)
		Object.MangleTimer++
		Object.MoveTime = 300
		rand(temp3, 50)
		Object.MoveTime += temp3
		if Object.MangleTimer >= Object.MoveTime
			Rand(Object.MangleRandom, 21)
				if Object.MangleAI > Object.MangleRandom 
					switch Object.VentState
						case VENT_START
							if Object.VentLocation >= 1
								Object.VentState = VENT_TOPLEFT_1
							else
								Object.VentState = VENT_MIDLEFT_6
							end if
						break
						case VENT_TOPLEFT_1
							if Object.VentLocation >= 1
								Object.VentState = VENT_TOPMIDLEFT_2
							else
								Object.VentState = VENT_MIDLEFT_6
							end if
						break			
						case VENT_TOPMIDLEFT_2
							if Object.VentLocation >= 1
								Object.VentState = VENT_TOPMIDDLE_3
							else
								Object.VentState = VENT_TOPLEFT_1
							end if
						break		
						case VENT_TOPMIDDLE_3
							if Object.VentLocation >= 1
								Object.VentState = VENT_MIDDLEMIDDLE_8
							else
								Object.VentState = VENT_TOPMIDRIGHT_4
							end if
						break		
						case VENT_TOPMIDRIGHT_4
							if Object.VentLocation >= 1
								Object.VentState = VENT_TOPRIGHT_5
							else
								Object.VentState = VENT_TOPMIDDLE_3
							end if
						break
						case VENT_TOPRIGHT_5
							if Object.VentLocation >= 1
								Object.VentState = VENT_TOPMIDRIGHT_4
							else
								Object.VentState = VENT_MIDRIGHT_10
							end if
						break
						case VENT_MIDLEFT_6
							switch Object.VentLocation
							case 0
								Object.VentState = VENT_TOPLEFT_1
							break
							case 1
								Object.VentState = VENT_MIDMIDLEFT_7
							break	
							case 2
								Object.VentState = VENT_BOTTOMLEFT_11
							break
							end switch
						break		
						case VENT_MIDMIDLEFT_7
							if Object.VentLocation >= 1
								Object.VentState = VENT_MIDDLEMIDDLE_8
							else
								Object.VentState = VENT_MIDLEFT_6
							end if
						break		
						case VENT_MIDMIDRIGHT_9
							if Object.VentLocation >= 1
								Object.VentState = VENT_MIDDLEMIDDLE_8
							else
								Object.VentState = VENT_MIDRIGHT_10	
							end if
						break	
						case VENT_MIDRIGHT_10
							if Object.VentLocation >= 1
								Object.VentState = VENT_MIDMIDRIGHT_9
							else
								Object.VentState = VENT_BOTTOMRIGHT_15	
							end if
						break	
						case VENT_BOTTOMLEFT_11
							if Object.VentLocation > 0
								Object.VentState = VENT_MIDLEFT_6
							else
								Object.VentState = VENT_BOTTOMMIDLEFT_12	
							end if
						break						
						case VENT_BOTTOMRIGHT_15
							if Object.VentLocation >= 0
								Object.VentState = VENT_BOTTOMMIDRIGHT_14
							else
								Object.VentState = VENT_MIDRIGHT_10	
							end if
						break

						//leads up to vent doors
						case VENT_MIDDLEMIDDLE_8
							PlaySfx(SfxName[VentQuiet1], 0)			
							object.state++
							Object.VentState = VENT_MIDDOOR
						break	
						case VENT_BOTTOMMIDLEFT_12	//guartnee door here
							object.state++
							PlaySfx(SfxName[VentQuiet1], 0)		
							Object.VentState = VENT_LEFTDOOR
						break			
						case VENT_BOTTOMMIDRIGHT_14	//guartnee door here
							object.state++
							PlaySfx(SfxName[VentQuiet1], 0)		
							Object.VentState = VENT_RIGHTDOOR
						break
							
					end switch
			end if

			Object.MangleTimer = 0
		end if
	break
	end switch
end if	
end event

event ObjectDraw

//	DrawNumbers(0, 32, 96, Object.VentState, 3, 8, 0)

//	DrawNumbers(0, 64, 96, Object.State, 3, 8, 0)

//	DrawNumbers(0, 96, 96, LeftVentDoor, 3, 8, 0)

	
	rand(Object.HeadShake, 8)

	if Mangle.state == 17
		object.drawOrder = 5
		temp1 = Screen.xsize
		temp1 /= 2
		temp1 -= 140
		
		temp2 = temp1
		temp2 += Object.HeadShake

		DrawSpriteScreenFX(10, FX_SCALE, temp2, Object.MangleRise)	//body
		DrawSpriteScreenFX(11, FX_SCALE, temp2, Object.MangleRise)	//head
	end if

	object.inkeffect = 2
	switch Mangle.state
	case 2
		object.alpha = 0
	break
	case 15
		if object[6].state != 2
			if object.alpha < 255
				object.alpha += 16
			else
				object.alpha = 255
			end if
			DrawSpriteScreenFX(13, FX_INK, Object.ScrollPos, -32)
		end if
	break
	end switch

end event


event ObjectStartup
	
	LoadSpriteSheet("FNAF/Jumpscares.gif")
	
	// Place the HUD object into reserved object slot 4 and initialise its values
	Object[27].Type = TypeName[Mangle]
	
	// The object should always be run, regardless if it's within camera bounds or not
	Object[27].Priority = PRIORITY_ACTIVE
	
	// Make the HUD draw ontop of all other sprites, too
	Object[27].DrawOrder = 4

	// 0-9 - Numbers 0-9
	SpriteFrame(0, 0, 8, 13, 558, 1)
	SpriteFrame(0, 0, 8, 13, 567, 1)
	SpriteFrame(0, 0, 8, 13, 576, 1)
	SpriteFrame(0, 0, 8, 13, 585, 1)
	SpriteFrame(0, 0, 8, 13, 594, 1)
	SpriteFrame(0, 0, 8, 13, 558, 15)
	SpriteFrame(0, 0, 8, 13, 567, 15)
	SpriteFrame(0, 0, 8, 13, 576, 15)
	SpriteFrame(0, 0, 8, 13, 585, 15)
	SpriteFrame(0, 0, 8, 13, 594, 15)

	SpriteFrame(0, 90, 316, 120, 1703, 1)
	SpriteFrame(46, 120, 206, 194, 1496, 1)	//11

	SpriteFrame(0, 0, 15, 21, 495, 1) //12

	//spring trap eyes
	SpriteFrame(226, 30, 150, 127, 1345, 1)		//13	

end event


// ========================
// Editor events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("3D_Test/Objects.gif")
	SpriteFrame(0, 0, 24, 16, 206, 58)

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
